/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.material.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.common.annotation.ServiceLog;
import org.springblade.common.constant.TrainingOperateConstant;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.classes.bo.UserBindingBo;
import org.springblade.modules.classes.service.IUserBindingService;
import org.springblade.modules.material.entity.PaperAnswerEntity;
import org.springblade.modules.material.excel.ReleaseExamUserImporter;
import org.springblade.modules.material.service.IAnswerService;
import org.springblade.modules.personal.service.IUserBaseService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.material.entity.ReleaseExamUserEntity;
import org.springblade.modules.material.vo.ReleaseExamUserVO;
import org.springblade.modules.material.excel.ReleaseExamUserExcel;
import org.springblade.modules.material.wrapper.releaseExamUserWrapper;
import org.springblade.modules.material.service.IReleaseExamUserService;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.tool.constant.BladeConstant;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Map;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
 * 考试学员表 控制器
 *
 * @author junxin
 * @since 2023-05-04
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-releaseExamUser/releaseExamUser")
@Api(value = "考试学员表", tags = "考试学员表接口")
public class ReleaseExamUserController extends BladeController {

	private final IReleaseExamUserService releaseExamUserService;
	private final IUserBaseService userBaseService;
	private final IUserBindingService userBindingService;
	private final IAnswerService answerService;
	/**
	 * 考试学员表 详情
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入releaseExamUser")
	public R<ReleaseExamUserVO> detail(ReleaseExamUserEntity releaseExamUser) {
		ReleaseExamUserEntity detail = releaseExamUserService.getOne(Condition.getQueryWrapper(releaseExamUser));
		return R.data(releaseExamUserWrapper.build().entityVO(detail));
	}

	/**
	 * 考试学员表 自定义分页
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入releaseExamUser")
	public R<IPage<ReleaseExamUserVO>> page(ReleaseExamUserVO releaseExamUser, Query query) {
		IPage<ReleaseExamUserVO> pages = releaseExamUserService.selectReleaseExamUserPage(Condition.getPage(query), releaseExamUser);
		return R.data(pages);
	}

	/**
	 * 授权修改
	 */
	@PostMapping("/permit-update")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "授权修改", notes = "传入releaseExamUser")
	@ServiceLog(value = TrainingOperateConstant.EXAM,operate = TrainingOperateConstant.UPDATE,describe = "授权修改")
	public R permitUpdate(@RequestBody ReleaseExamUserVO releaseExamUser) {
		LambdaUpdateWrapper<ReleaseExamUserEntity> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
		lambdaUpdateWrapper.eq(ReleaseExamUserEntity::getReleaseId,releaseExamUser.getReleaseId());
		lambdaUpdateWrapper.eq(ReleaseExamUserEntity::getUserId,releaseExamUser.getUserId());
		lambdaUpdateWrapper.set(ReleaseExamUserEntity::getPermit,releaseExamUser.getPermit());
		return R.data(releaseExamUserService.update(lambdaUpdateWrapper));
	}

	/**
	 * 重置该考生的考试状态
	 * 可重新进入考试
	 */
	@GetMapping("/reset-exam-status")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "重置考试状态", notes = "传入releaseExamUser")
	@ServiceLog(value = TrainingOperateConstant.EXAM,operate = TrainingOperateConstant.UPDATE,describe = "重置考试状态")
	public R resetExamStatus(@RequestBody ReleaseExamUserVO releaseExamUser) {
		PaperAnswerEntity oldAnswerEntity = answerService.getOne(Wrappers.<PaperAnswerEntity>query().lambda()
			.eq(PaperAnswerEntity::getUserId, releaseExamUser.getUserId())
			.eq(PaperAnswerEntity::getPaperReleaseId, releaseExamUser.getReleaseId())
			.eq(PaperAnswerEntity::getExamineType,2));
		if (null!=oldAnswerEntity){
			answerService.removeById(oldAnswerEntity);
		}
		return R.success("重置成功");
	}

	/**
	 * 考试学员表 新增
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "")
	@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
	public R save(@Valid @RequestBody UserBindingBo bo) {
		userBindingService.addUserBinding(bo);
		return R.success("保存成功");
	}

	/**
	 * 考试学员表 修改
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入releaseExamUser")
	public R update(@Valid @RequestBody ReleaseExamUserEntity releaseExamUser) {
		return R.status(releaseExamUserService.updateById(releaseExamUser));
	}


	/**
	 * 考试学员表 删除
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(releaseExamUserService.deleteLogic(Func.toLongList(ids)));
	}


	/**
	 * 导出数据
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@GetMapping("/export-releaseExamUser")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入releaseExamUser")
	public void exportreleaseExamUser(@ApiIgnore @RequestParam Map<String, Object> releaseExamUser, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<ReleaseExamUserEntity> queryWrapper = Condition.getQueryWrapper(releaseExamUser, ReleaseExamUserEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(releaseExamUser::getTenantId, bladeUser.getTenantId());
		//}
		queryWrapper.lambda().eq(ReleaseExamUserEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<ReleaseExamUserExcel> list = releaseExamUserService.exportreleaseExamUser(queryWrapper);
		ExcelUtil.export(response, "考试学员表数据" + DateUtil.time(), "考试学员表数据表", list, ReleaseExamUserExcel.class);
	}


	/**
	 * 批量导入班级学员
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@PostMapping("import-user")
	@ApiOperationSupport(order = 12)
	@ApiOperation(value = "导入考试学员", notes = "传入releaseId")
	public R importQuestionBank(MultipartFile file, Long releaseId) {
		if (null==releaseId) throw new ServiceException("请选择考试");
		ReleaseExamUserImporter releaseExamUserImporter = new ReleaseExamUserImporter(releaseExamUserService,userBaseService,releaseId);
		ExcelUtil.save(file, releaseExamUserImporter, ReleaseExamUserExcel.class);
		return R.success("操作成功");
	}

	/**
	 * 导出模板
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@GetMapping("export-template")
	@ApiOperationSupport(order = 13)
	@ApiOperation(value = "导出模板")
	public void exportUser(HttpServletResponse response) {
		List<ReleaseExamUserExcel> list = new ArrayList<>();
		ExcelUtil.export(response, "考试学员数据模板", "考试学员数据表", list, ReleaseExamUserExcel.class);
	}


}
